import os
import config
import explain
import wHtml

print("Start......")
print("By:Lamzy")
path = config.getPath()
fatherFiles = os.listdir(path)
for fatherFile in fatherFiles:
    if os.path.isfile(path + '\\' + fatherFile):  # 不是文件夹
        continue
    files = os.listdir(path + '\\' + fatherFile)  # 遍历文件
    for file in files:
        if os.path.isfile(path + '\\' + fatherFile + "\\" + file):  # 不是文件夹
            continue
        # path + '\\' + fatherFile + "\\" + file
        # F:\DeskTop\Temp\合泰\HT32_STD_5xxxx_FWLib_v009_3383\example\ADC\AnalogWatchdog
        if not os.path.exists(path + '\\' + fatherFile + "\\" + file + "\\readme.txt"):
            continue  # 文件不存在
        # 文件存在，读取内容
        t = open(path + '\\' + fatherFile + "\\" + file + "\\readme.txt", 'r', encoding='utf-8')
        text = t.read()
        extext = explain.run(text)  # 进行翻译
        if len(extext) <= 5:
            #翻译失败
            err = open(path + "\\error.txt", 'a+')
            err.write('翻译失败:' + path + '\\' + fatherFile + "\\" + file + "\\readme.txt\n")
            err.close()
        outzh = open(path + '\\' + fatherFile + "\\" + file + "\\readme_zh.txt", 'w', encoding='utf-8')
        outzh.write(extext)
        outzh.close()
        print('完成' + path + '\\' + fatherFile + "\\" + file + "\\readme_zh.txt")
        wHtml.addIndex(file, extext)
    wHtml.addFatherIndex(fatherFile)
wHtml.save()
print("End......")